package dk.sdu.flexibus.communications;

import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;

public class CancelRequestProvider extends AbstractCommunicationsProvider {

	private int serverId;
	private CancelRequestListener listener;

	public CancelRequestProvider(int serverId,
			CancelRequestListener listener) {
		this.serverId = serverId;
		this.listener = listener;
	}
	
	@Override
	protected void composeMessage(SoapObject request) {
		request.addProperty("transportID", serverId);
	}

	@Override
	protected String getMethodName() {
		return "CancelRequest";
	}

	@Override
	protected String getSoapAction() {
		return "http://www.madis.dk/Uni/FlexiBus/CancelRequest";
	}

	@Override
	protected CallServiceTask getCallServiceTask() {
		return new RequestWithFixedDropOffTimeCallTask();
	}

	@Override
	protected boolean getDebugSetting() {
		return true;
	}

	private class RequestWithFixedDropOffTimeCallTask extends CallServiceTask {

		@Override
		protected void onPostExecute(SoapSerializationEnvelope envelope) {
			try {
				String resultString;
				SoapPrimitive response = (SoapPrimitive) envelope.getResponse();
				resultString = response.toString();
				boolean result = Boolean.parseBoolean(resultString);
				listener.sendCancelResult(result);
			} catch (Exception exception) {
				listener.sendError(exception);
			}
		}
	}
}
